package nciae.db.conf;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import nciae.db.pojo.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class MyMvcConfiguration implements WebMvcConfigurer{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:D:/npt/npt/src/main/resources/upload/");
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SessionHandlerInterceptor())
                .addPathPatterns("/")
                .addPathPatterns("/index/**")
                .addPathPatterns("/user/**")
                .addPathPatterns("/dict/**")
                .addPathPatterns("/project/**")
                .addPathPatterns("/sysuser/**")
                .addPathPatterns("/looppic/**")
                .addPathPatterns("/student/**")
                .addPathPatterns("/class/**")
                .addPathPatterns("/homework/**")
                .addPathPatterns("/homePage/**")
                .addPathPatterns("/status/**")
                .addPathPatterns("/category/**")
                .addPathPatterns("/pretraining/*")
                .addPathPatterns("/log/**")
                .addPathPatterns("/settings/**")
                .addPathPatterns("/admin/**");
    }

    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件
        PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();
        // 设置请求的页面大于最大页后操作，true调回到首页，false继续请求。默认false
        pageInterceptor.setOverflow(false);
        // 单页分页条数限制，默认无限制
        pageInterceptor.setMaxLimit(500L);
        // 设置数据库类型
        pageInterceptor.setDbType(DbType.MYSQL);
        interceptor.addInnerInterceptor(pageInterceptor);
        return interceptor;
    }

    class SessionHandlerInterceptor implements HandlerInterceptor {

        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            System.out.println("session check");
            User user = (User) request.getSession().getAttribute("user");
            if(user==null){
                response.sendRedirect(("/login"));
                return false;
            }
            return true;
        }
    }
}

